<?php

/**
 * order actions.
 *
 * @package    emc
 * @subpackage order
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class orderActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }

  public function executeList(sfWebRequest $request)
  {
  	$this->user_id = $request->getParameter('user_id', null);
  	$this->is_new = $request->getParameter('is_new', null);
  	$this->notify = $request->getParameter('notify', null);
  	$q = Doctrine_Query::create()
  		->select('cl.id as order_id, cl.user_id as user_id, cl.delivery_address as delivery_address, cl.payment_method as payment_method, cl.created_at as created_at, u.first_name as user_first_name, u.last_name as user_last_name, u.email_address as user_email, cl.status as order_status')
  		->from('ClientOrder cl')
  		->leftJoin('cl.User u');

  	if ($this->user_id)
  		$q->addWhere('u.id = ?', $this->user_id);

  	if ($this->is_new)
  		$q->addWhere('cl.status = ?', $this->is_new);

  	$this->orders = $q->fetchArray();

  	return sfView::SUCCESS;
  }

  public function executeProductOrderList(sfWebRequest $request)
  {
  	$this->order_id = $request->getParameter('order_id', null);
  	$this->order = Doctrine_Core::getTable('ClientOrder')->find($this->order_id);
  	if ($this->order_id && $this->order) {
  		$order_products = $this->order->Products;
  		$this->order_details = array();
  		foreach ($order_products as $order_product) {
  			$product = Doctrine_Core::getTable('Product')->find($order_product->product_id);
  			$order_detail['product_id'] = $product->id;
  			$order_detail['product_name'] = $product->name;

  			$order_detail['quantity'] = $order_product->quantity;
  			if ($order_product->option_id) {
          $option = Doctrine_Core::getTable('ProductOption')->find($order_product->option_id);
          $order_detail['option_name'] = $option->name;
          $order_detail['option_id'] = $option->id;
        } else {
          $order_detail['option_name'] = 'no option';
        }
  				
  			$order_detail['created_at'] = $order_product->created_at;
        $order_detail['status'] = $order_product->status;
        $marque = $this->retriveMarque($order_product->Product);
  			$this->order_details[$marque->id]['name'] = $marque->name;
  			$this->order_details[$marque->id]['list'][] = $order_detail;
  		}
  	} else {
  		$this->redirect('@order_list?notify=order_not_found');
  	}
  }

  private function retriveMarque($product)
  {
    $category = $product->Category;
    while ($category->parent_id != 1) {
      $category = Doctrine_Core::getTable('Category')->find($category->parent_id);
    }
    $marque = Doctrine_Core::getTable('Marque')->findOneByName($category->name);
    return $marque;
  }

  public function executePlace(sfWebRequest $request)
  {
    $marque_id = $request->getParameter('marque_id', null);
    $product_list = $request->getParameter('product_list', null);
    $order_id = $request->getParameter('order_id', null);
    if ($marque_id && $product_list) {
      $jobboard = Doctrine_Core::getTable('Marque')->find($marque_id)->Jobboard;
      if ($jobboard) {
        $result = $jobboard->placeOrder($product_list, $order_id);
      }
      
      
      $this->renderText(var_dump($result));
    }
    return sfView::NONE;
  }

}
